package com.tensquare.article.controller;

import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Macross
 * @Type ArticleController.java
 * @Desc 文章信息管理控制类
 * @date 2019/12/17 19:34
 */
@RestController
@RequestMapping("/article")
@Api(value = "文章信息管理控制类")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 指定文章审核
     * @param articleId 文章id
     * @return
     */
    @ApiOperation(value = "审核文章",notes = "返回审核文章是否成功")
    @PutMapping("/examine/{articleId}")
    public Result examine(@PathVariable @ApiParam(value = "文章id",required = true) String articleId){
        //指定文章审核
        articleService.examine(articleId);
        //返回请求结果
        return new Result(true,StatusCode.OK,"操作成功");
    }

    /**
     * 文章点赞
     * @param articleId 文章id
     * @return
     */
    @PutMapping("/thumbup/{articleId}")
    @ApiOperation(value = "文章点赞",notes = "文章对应的点赞数+1")
    public Result updateThumbup(@PathVariable @ApiParam(value = "文章id",required = true) String articleId){
        //文章点赞数+1
        articleService.updateThumbup(articleId);
        //返回请求结果
        return new Result(true,StatusCode.OK,"操作成功");
    }

    /**
     * 获取指定id的文章信息
     * @param articleId 文章id
     * @return
     */
    @GetMapping("/{articleId}")
    @ApiOperation(value = "获取文章",notes = "获取指定的文章id的文章信息")
    public Result<Article> findById(@PathVariable @ApiParam(value = "文章id",required = true) String articleId){
        //获取指定的文章信息
        Article article = articleService.findById(articleId);
        //将获取结果进行包装并返回
        return new Result<>(true,StatusCode.OK,"查询成功",article);
    }

    /**
     * 更新文章
     * @param articleId 文章id
     * @param article 文章信息
     * @return
     */
    @PutMapping("/{articleId}")
    @ApiOperation(value = "更新文章",notes = "更新数据库文章信息,并清除对应的文章信息缓存")
    public Result updateArticle(@PathVariable @ApiParam(value = "文章id",required = true) String articleId,
                                @RequestBody @ApiParam(value = "文章信息",required = true) Article article){
        //绑定文章id
        article.setId(articleId);
        //更新文章
        articleService.updateArticle(article);
        //返回请求结果
        return new Result(true,StatusCode.OK,"操作成功");
    }

    /**
     * 删除文章
     * @param articleId 文章id
     * @return
     */
    @DeleteMapping("/{articleId}")
    @ApiOperation(value = "删除文章",notes = "删除数据库文章信息,并清除对应的文章信息缓存")
    public Result deleteArticle(@PathVariable @ApiParam(value = "文章id",required = true) String articleId){
        //删除文章
        articleService.deleteArticle(articleId);
        //返回请求结果
        return new Result(true,StatusCode.OK,"操作成功");
    }
}
